#define TAG "native_libmad"   
  
  
#include "FileSystem.h"   
#include <stdlib.h>   
#include <jni.h>   
#include <android/log.h>   
  
  
#define LOG_TAG "libmad"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)  
   
 extern int64_t NativeMP3Decoder_readSamples( short *target, int64_t size);  
   
 extern void  NativeMP3Decoder_closeAduioFile();  
   
 extern int64_t NativeMP3Decoder_getAduioSamplerate();  
   
 extern int64_t NativeMP3Decoder_init(char * filepath,int64_t start,int64_t);  
 
  extern int64_t NativeMP3Decoder_seek(int64_t start,int64_t len);  
  
  
  
JNIEXPORT jlong JNICALL 
Java_com_afei_oid_Oiddecoder_initAudioPlayer(JNIEnv *env, jobject obj, jstring file,jint startAddr,jlong size)
{  
      
    const char* fileString = (*env)->GetStringUTFChars(env,file, NULL);  
    LOGI("initAudioPlayer");
    return  NativeMP3Decoder_init((char*)fileString,startAddr,size);  
  
}  
JNIEXPORT jlong JNICALL 
Java_com_afei_oid_Oiddecoder_seekPlayPos(JNIEnv *env,jobject obj,jlong pos,jlong len)
{  
	//__android_log_print(ANDROID_LOG_DEBUG, TAG, "Oiddecoder_seekPlayPos,pos=%lld,len=%lld",pos,len);
    return NativeMP3Decoder_seek(pos,len);
}  

JNIEXPORT jint JNICALL
Java_com_afei_oid_Oiddecoder_getAudioBuf(JNIEnv *env, jobject obj ,jshortArray audioBuf,jint len)
{  
    int bufsize = 0;  
    int ret = 0;  
    jshort *_buf = (*env)->GetShortArrayElements(env, audioBuf, 0); 
    if (_buf != NULL) {  
        bufsize = (*env)->GetArrayLength(env, audioBuf);  
        memset(_buf, 0, bufsize*2);  
        ret = NativeMP3Decoder_readSamples(_buf, len);  
        (*env)->ReleaseShortArrayElements(env, audioBuf, _buf, 0);  
    }  
    else{  
  
            __android_log_print(ANDROID_LOG_DEBUG, TAG, "getAudio failed");  
        }  
    return ret;  
}  


JNIEXPORT jint JNICALL   
Java_com_afei_oid_Oiddecoder_getAudioSamplerate()
{  
    return NativeMP3Decoder_getAduioSamplerate();  
}  
  
JNIEXPORT void JNICALL 
Java_com_afei_oid_Oiddecoder_closeAduioFile( )
  
{  
    NativeMP3Decoder_closeAduioFile();  
  
}  
